var pdtBody = $("#pdtBody");
ns = 'arm-month-product-reporter!';
var opts = new Array();
$(function(){
	initiControlls();
	//$.datepicker.setDefaults($.datepicker.regional['zh-CN']);
	//$("#rdateString").datepicker({dateFormat : 'yy-mm-dd'});
});
function initiControlls() {
	
	$("#showbtn").bind("click",showAddDialog);
	
}

function showAddDialog(){
	var url = "../armmonthreporter/prepend";
	window.showModalDialog(url,window);
}

// 删除指定索引行
function deleteRow(index) {
	var objTable = document.getElementById('itemlist');
	objTable.deleteRow(index);
}

function deleteRowById(rid) {
	var row = document.getElementById(rid).rowIndex;
	deleteRow(row);
}

function addDataLine(hostCode,hostName,local,submit,remark) {
	itemIndex++;
	var armPrefix = "bean.armItems[" + itemIndex + "].";
	var datas = [ {
		"type" : "text",
		"attrs" : {
			"name" : armPrefix + "host.code",
			"value":hostCode,
			"readOnly":true
		}
	}, {
		"type" : "text",
		"attrs" : {
			"name" : armPrefix + "host.name",
			"value":hostName,
			"readOnly" : true
		}
	},
	// 当月动工
	{
		"type" : "text",
		"attrs" : {
			"name" : fieldValueName("local", itemIndex,"atomValue"),
			"value" : local
		},
		"events" : {
			"blur" : function() {
				return judgeNumber(this);
			}
		},
		"className":"number-field"
			
	}, {
		"type" : "label",
		"attrs" : {
			"text" : 0
		}
	} ,
	
	// 合格
	{
		"type" : "text",
		"attrs" : {
			"name" : fieldValueName("submit", itemIndex,"atomValue"),
			"value" : submit
		},
		"events" : {
			"blur" : function() {
				return judgeNumber(this);
			}
		},
		"className":"number-field"
	}, {
		"type" : "label",
		"attrs" : {
			"text" : 0
		}
	} ,	
	// 备注
	{
		"type" : "text",
		"attrs" : {
			"name" : armPrefix + "remark",
			"value":remark,
			"title":remark
		},
		"className":"number-field remarkTip"
	}, {
		"type" : "img",
		"attrs" : {
			"src" : sbase + "/images/removeme.gif"
		},
		"csses" : {
			"cursor" : "hand"
		},
		"events" : {
			"click" : function() {
				$(this).parent().parent().remove();
			}
		}
	}

	];
	addDataItem(datas, pdtBody);
	
}


function ajaxSubmit() {
	var postdata = $("form").serialize();
	var dataurl = '../armmonthreporter/arm-month-product-reporter!'+method;

	$.ajax({
		type : "POST",
		url : dataurl,
		data : postdata,
		success : function(msg) {
			alert("数据保存成功");
		},
		error : function(msg) {
			alert("保存出现错误:\n" + msg);
		}
	});

}

function subData(formid) {
	// testForm();
	if(confirm("您确认保存当前更改?")) {		
		ajaxSubmit();
	}
}

function subMofidy(formid) {
	
}


function testForm() {
	var form = document.getElementById('armform');
	var msg = '';
	for(var i = 0; i < form.elements.length; i++) {
		msg += form.elements[i].name + ' = ' + form.elements[i].value + '<br/>';
	}
	$("#debug").html(msg);

}

function test() {
	var trs = $("tbody[id='pdtBody']>tr");
	$("#debug").html($("form").serialize());
}